//http://developer.android.com/resources/tutorials/notepad/notepad-ex2.html
//http://developer.android.com/resources/tutorials/notepad/notepad-ex3.html
//http://developer.android.com/resources/faq/commontasks.html#localhostalias
//http://developer.android.com/resources/browser.html?tag=sample
package com.jinarusha.shareprayers;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;

import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.AdapterView.AdapterContextMenuInfo;

public class MyPrayersActivity extends ListActivity {
	private static final int ACTIVITY_CREATE = 0;
	private static final int ACTIVITY_EDIT = 1;

	public static final int ADDPRAYER_ID = Menu.FIRST;
	public static final int DELETEPRAYER_ID = Menu.FIRST + 1;

	private int prayerNumber = 0;
	private ListItemDbAdapter dbHelper;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		dbHelper = new ListItemDbAdapter(this);
		dbHelper.open();
		fillData();
		registerForContextMenu(getListView());
		// setContentView(R.layout.myprayers_layout);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		boolean result = super.onCreateOptionsMenu(menu);
		menu.add(0, ADDPRAYER_ID, 0, R.string.menu_addPrayer);
		return result;
	}

	private void createNote() {
		Intent i = new Intent(this, EditPrayerActivity.class);
		startActivityForResult(i, ACTIVITY_CREATE);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode,
			Intent intent) {
		super.onActivityResult(requestCode, resultCode, intent);

		fillData();

	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		switch (item.getItemId()) {
		case ADDPRAYER_ID:
			createNote();
			return true;
		}

		return super.onMenuItemSelected(featureId, item);
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		menu.add(Menu.NONE, DELETEPRAYER_ID, 0, R.string.menu_deletePrayer);

	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case DELETEPRAYER_ID:
			AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
					.getMenuInfo();
			System.out.println("id: " + info.id);
			dbHelper.deleteNote(info.id);
			fillData();
			return true;
		}
		return super.onContextItemSelected(item);
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);

		Intent i = new Intent(this, EditPrayerActivity.class);
		i.putExtra(ListItemDbAdapter.KEY_ROWID, id);
		startActivityForResult(i, ACTIVITY_EDIT);
	}

	private void fillData() {

		// Get all of the rows from the database and create the item list
		Cursor prayerItemCursor = dbHelper.fetchAllNotes();
		startManagingCursor(prayerItemCursor);

		// Create an array to specify the fields we want to display in the list
		// (only TITLE)
		String[] from = new String[] { ListItemDbAdapter.KEY_TITLE };

		// and an array of the fields we want to bind those fields to (in this
		// case just text1)
		int[] to = new int[] { R.id.text1 };

		// Now create a simple cursor adapter and set it to display
		SimpleCursorAdapter notes = new SimpleCursorAdapter(this,
				R.layout.prayerlistrow, prayerItemCursor, from, to);
		setListAdapter(notes);

	}

}
